package com.jeecg.p3.system.poster.web;

import com.jeecg.p3.base.util.ComCode;
import com.jeecg.p3.base.util.DateUtil;
import com.jeecg.p3.base.util.UploadUtil;
import com.jeecg.p3.system.base.BaseController;
import com.jeecg.p3.system.entity.JwSystemUser;
import com.jeecg.p3.system.poster.entity.PosterType;
import com.jeecg.p3.system.poster.service.PosterTypeService;
import com.jeecg.p3.system.service.JwSystemUserService;
import com.jeecg.p3.system.util.Constants;
import com.jeecg.p3.system.util.HtmlUtil;
import com.jeecg.p3.system.vo.LoginUser;
import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 海报设计类型
 */
@Controller
@RequestMapping("/system/back/posterType/manage")
public class PosterTypeController extends BaseController {
    public final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private PosterTypeService service;
    @Autowired
    private JwSystemUserService userService;

    /**
     *
     * @param query 海报设计查询对象
     * @param request
     * @param response
     * @param pageNo 第几页
     * @param pageSize 页数大小
     * @throws Exception
     */
    @RequestMapping(value="/list.do",method = {RequestMethod.GET,RequestMethod.POST})
    public void toIndividual(@ModelAttribute PosterType query, HttpServletRequest request, HttpServletResponse response,
                             @RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
                             @RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception{
        PageQuery<PosterType> pageQuery = new PageQuery<>();
        pageQuery.setPageNo(pageNo);
        pageQuery.setPageSize(pageSize);
        VelocityContext velocityContext = new VelocityContext();
        pageQuery.setQuery(query);
        velocityContext.put("PosterType",query);
        velocityContext.put("pageInfos", SystemTools.convertPaginatedList(service.queryPageList(pageQuery)));
        String viewName = "system/back/poster/poster_type.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 跳转到新增海报设计界面页面
     * @return
     */
    @RequestMapping(value = "/toAdd",method ={RequestMethod.GET, RequestMethod.POST})
    public void toAddDialog(HttpServletRequest request, HttpServletResponse response, ModelMap model)throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        String viewName = "system/back/poster/poster_type_add.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 
     * 保存海报设计信息
     * @return
     */
    @RequestMapping(value = "/doAdd",method ={RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public AjaxJson doAdd(@ModelAttribute PosterType posterType, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            LoginUser loginUser = getCurrentUser(request);
            PosterType pt = service.findByCode(posterType.getCode());
            if(pt != null) {
                j.setSuccess(false);
                j.setMsg("存在相同code，保存失败");
                return j;
            }
            posterType.setCreateDate(DateUtil.dateToString(new Date()));
            posterType.setCreateTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
            posterType.setCreateUser(loginUser.getId());
            service.doAdd(posterType);
            j.setMsg("保存成功");
        } catch (Exception e) {
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("保存失败");
        }
        return j;
    }

    /**
     * 海报设计类型文件上传
     * @param request
     * @param response
     * @param file
     * @return
     */
    @RequestMapping(value = "/doUpload",method ={RequestMethod.POST})
    @ResponseBody
    public AjaxJson doUpload(MultipartHttpServletRequest request, HttpServletResponse response, MultipartFile file){
        LoginUser user = getCurrentUser(request);
        AjaxJson j = new AjaxJson();
        try {
            String fileUrl =  ComCode.separator + uploadConfig.getStaticPath() + ComCode.separator + user.getId() + ComCode.posterType;
            String fileDir = uploadConfig.getUploadPath()  + user.getId() + ComCode.posterType;
            String url = upload(fileUrl, fileDir, file);
            if(url != null) {
                j.setObj(url);
                j.setMsg("保存成功");
            } else {
                j.setSuccess(false);
                j.setMsg("保存失败，未找到源文件！");
            }
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("保存失败" + e.toString());
            log.error(e.toString());
            e.printStackTrace();
        }
        return j;
    }
}
